package com.ouzexi.finalexp

import android.content.Intent
import android.os.*
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import com.google.gson.Gson
import com.ouzexi.finalexp.util.save
import com.ouzexi.finalexp.vo.Student
import kotlinx.android.synthetic.main.activity_mine.*
import okhttp3.*
import java.io.IOException

class mine : AppCompatActivity() {
    lateinit var token:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mine)

        token = save().getData(this,"token")

        getUserData()


        // 点击推荐岗位跳转
        red.setOnClickListener{
            val intent = Intent(this, RecIndex::class.java)
            startActivity(intent)
        }

        // 点击心仪岗位跳转
        admire.setOnClickListener{
            val intent = Intent(this, admiring_position::class.java)
            startActivity(intent)
        }

        // 点击修改信息跳转
        modify.setOnClickListener{
            val intent = Intent(this, modifyInfo::class.java)
            startActivity(intent)
        }

        // 点击修改密码跳转
        modify_password.setOnClickListener{
            val intent = Intent(this, modifyPassword::class.java)
            startActivity(intent)
        }
    }
    fun initView(result: String?) {
        val student = Gson().fromJson(result, Student::class.java)
        runOnUiThread{
            if (student.data.nickname.isEmpty()) {
                student_name.text = "请填写"
            } else {
                student_name.text = student.data.nickname
            }
            if (student.data.major.isEmpty()) {
                student_major.text = "请填写"
            } else {
                student_major.text= student.data.major
            }
            student_school.text = student.data.sid.toString()
            student_email.text = student.data.username
        }
    }

    private fun getUserData() {
        val urlAPI = "http://10.0.2.2:8080/school/student/getUserInfo"
        Log.i("minetoken", token)
        val request = Request.Builder()
            .url(urlAPI)
            .addHeader("token", token)
            .get()
            .build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                initView(result)
            }
            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
            }
        })

    }
}